<template>
 <a-modal v-model:visible="visible" title="标签" width="calc(100% - 20px)" :mask-closable="false" :modal-style="{ maxWidth: '520px' }"
     @before-ok="save" @close="close">
    <a-form ref="formRef" :model="form" :rules="rules" size="medium" auto-label-width>
      <a-form-item label="标签名称" field="title">
        <a-input v-model.trim="form.title" placeholder="请输入标签名称" allow-clear :max-length="10"> </a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { fileTagSave,fileTagUpdate } from '@/apis/file'
import type { FormInstance } from '@arco-design/web-vue'
import { Message } from '@arco-design/web-vue'
const visible = ref<boolean>(false)
const formRef = useTemplateRef('formRef')

const form = ref({
  tagId: 0,
  title: '',
})
const rules: FormInstance['rules'] = {
  title: [
    { required: true, message: '请输入标签名称' },
  ],

  isAdmin: [{ required: true }]
}


const emits = defineEmits(['success'])

const open = (rows) => {
  form.value = {
  tagId: 0,
  title: '',
}
if(rows?.tagId){
  form.value = rows
}
  visible.value = true
}

const close = () => {
  visible.value = false
}
const save = async () => { 
  const valid = await formRef.value?.validate()
   if (valid) return false
   if(form.value.tagId){
    const res = await fileTagUpdate(form.value)
    Message.success('保存成功')
   }else{
    const res = await fileTagSave(form.value)
    Message.success('保存成功')
   }
   emits('success')

}

defineExpose({ open })

</script>